package com.blocks.mq.producer.controller;

import com.alibaba.fastjson.JSON;
import com.blocks.mq.producer.reader.impl.DatebaseReader;
import com.blocks.mq.producer.reader.impl.ExcelReader;
import com.blocks.mq.producer.reader.impl.TXTReader;
import com.zut.blocks.base.mq.Message;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Queue;
import javax.jms.Topic;
import java.io.File;
import java.io.IOException;

/**
 * @author
 */
@RestController
public class ProducerController {
    private final JmsMessagingTemplate jmsMessagingTemplate;

    private final Queue queue;

    private final Topic topic;

    @Resource
    DatebaseReader datebaseReader;

    public ProducerController(JmsMessagingTemplate jmsMessagingTemplate, Queue queue, Topic topic) {
        this.jmsMessagingTemplate = jmsMessagingTemplate;
        this.queue = queue;
        this.topic = topic;
    }

    @PostMapping("/queue/test")
    public String sendQueue(@RequestBody Message msg) {
        this.sendMessage(this.queue, JSON.toJSONString(msg));
        return "success";
    }

    @PostMapping("/topic/test")
    public String sendTopic(@RequestBody Message msg) {
        this.sendMessage(this.topic, JSON.toJSONString(msg));
        return "success";
    }

    @GetMapping("/send/test")
    public String send(String path) throws IOException {
        File file = new File(path);
        if (file.exists()) {
            TXTReader t1 = new TXTReader();
            t1.reader(path);
            Message mg = new Message();
            mg.setData(t1.reader(path));
            mg.setType(Message.MessageType.txt);
            sendMessage(this.topic,JSON.toJSONString(mg));
            return "发送成功";
        }
        else {
            return "路径不存在，请重新输入！";
        }
    }
    @GetMapping("/send/excelData")
    public  String  sendexcel(String path) throws IOException {
        File file=new File(path);
        if(file.exists()){
            ExcelReader e1=new ExcelReader();
            e1.reader(path);
            Message mg=new Message();

            mg.setData(e1.reader(path));
            mg.setType(Message.MessageType.xlsx);


            sendMessage(this.topic,JSON.toJSONString(mg));
            return "发送成功";
        } else {
            return "路径不存在，请重新输入！";
        }
    }

    @GetMapping("/send/date")
    @ResponseBody
    public Message sendDateBaseMessage(String id) throws IOException {
        Message message = new Message();
        message.setData(datebaseReader.reader(id));
        message.setType(Message.MessageType.database);
        sendMessage(this.topic, JSON.toJSONString(message));
        return message;
    }

    private void sendMessage(Destination destination, final String message) {
        jmsMessagingTemplate.convertAndSend(destination, message);
    }

}